package com.hex.ds.hdrs.cron.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.common.page.PageData;
import com.hex.ds.hdrs.cron.converter.ICronTaskDefVoConverter;
import com.hex.ds.hdrs.cron.service.CronTaskDefService;
import com.hex.ds.hdrs.cron.vo.CronTaskDefVo;
import com.hex.ds.hdrs.cron.vo.vo.CronTaskDefQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.hex.ds.hdrs.cron.controller
 * @ClassName CronTaskDefController
 * @Description: 定时任务定义-WEB接口
 * @Author: wz.li
 * @Date 2023/4/3 14:26
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@RequestMapping("/api/cronTaskDef")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CronTaskDefController {

    private final CronTaskDefService cronTaskDefService;

    private final ICronTaskDefVoConverter converter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [queryVo, page] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：定时任务定义-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/3 16:41 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("分页查询定时任务定义")
    public ResponseEntity<Object> queryListByPage(CronTaskDefQueryVo queryVo, Page page) {
        log.debug("【CronTaskDefController.queryListByPage】分页查询定时任务定义，参数：{}", queryVo.toString());
        PageData data = cronTaskDefService.queryListByPage(converter.toCronTaskDefDto(queryVo), page);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    /*
     * @Method: add <br>
     * @Param: [cronTaskDefVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br> 
     * @Description：新增定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/3 16:47 <br>
     * @Version： V2.0.2<br>
     */
    @PostMapping
    @Log("新增定时任务定义")
    @ApiOperation("新增定时任务定义")
    public ResponseEntity<Object> add(@RequestBody CronTaskDefVo cronTaskDefVo) {
        log.info("【CronTaskDefController.add】新增定时任务定义，参数：{}", cronTaskDefVo.toString());
        cronTaskDefService.add(converter.toCronTaskDefDto(cronTaskDefVo));
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /*
     * @Method: update <br>
     * @Param: [cronTaskDefVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：更新定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/3 16:55 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping
    @Log("更新定时任务定义")
    @ApiOperation("更新定时任务定义")
    public ResponseEntity<Object> update(@RequestBody CronTaskDefVo cronTaskDefVo) {
        log.info("【CronTaskDefController.update】更新定时任务定义，参数：{}", cronTaskDefVo.toString());
        cronTaskDefService.modifyById(converter.toCronTaskDefDto(cronTaskDefVo));
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /*
     * @Method: batchDelete <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：批量删除定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 10:29 <br>
     * @Version： V2.0.2<br>
     */
    @DeleteMapping
    @Log("批量删除定时任务定义")
    @ApiOperation("批量删除定时任务定义")
    public ResponseEntity<Object> batchDelete(@RequestBody List<String> pkIds) {
        log.info("【CronTaskDefController.batchDelete】删除定时任务定义，参数：{}", pkIds.toString());
        cronTaskDefService.batchRemoveById(pkIds);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /*
     * @Method: batchEnable <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：批量启用定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 10:30 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping("/enable")
    @Log("批量启用定时任务定义")
    @ApiOperation("批量启用定时任务定义")
    public ResponseEntity<Object> batchEnable (@RequestBody List<String> pkIds) {
        log.info("【CronTaskDefController.batchEnable】启用定时任务定义，参数：{}", pkIds.toString());
        cronTaskDefService.batchEnableById(pkIds);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /*
     * @Method: batchDisable <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：批量停用定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 10:30 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping("/disable")
    @Log("批量停用定时任务定义")
    @ApiOperation("批量停用定时任务定义")
    public ResponseEntity<Object> batchDisable (@RequestBody List<String> pkIds) {
        log.info("【CronTaskDefController.batchDisable】停用定时任务定义，参数：{}", pkIds.toString());
        cronTaskDefService.batchDisableById(pkIds);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /*
     * @Method: queryByTaskCode <br>
     * @Param: [taskCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：根据任务编码查询定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 10:35 <br>
     * @Version： V2.0.2<br>
     */
    @ApiOperation("根据任务编码查询定时任务定义")
    @GetMapping("/{taskCode:.+}")
    public ResponseEntity<Object> queryByTaskCode(@PathVariable("taskCode") String taskCode) {
        log.debug("【CronTaskDefController.queryByTaskCode】查询指定编码: {}任务定义", taskCode);
        return new ResponseEntity<>(cronTaskDefService.queryByTaskCode(taskCode), HttpStatus.OK);
    }

    /*
     * @Method: upload <br>
     * @Param: [request] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：导入定时任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 10:31 <br>
     * @Version： V2.0.2<br>
     */
    @Log("导入定时任务定义")
    @ApiOperation("导入定时任务定义")
    @PostMapping(value = "/upload")
    public ResponseEntity<Object> upload(HttpServletRequest request) {
        log.info("【CronTaskDefController.upload】导入");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map resultMap = cronTaskDefService.importExcel(multipartRequest.getFile("file"),
                request.getParameter("batchTag"), request.getParameter("appCode"));
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }


    /*
     * @Method: download <br>
     * @Param: [queryVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：导出任务定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 10:31 <br>
     * @Version： V2.0.2<br>
     */
    @Log("导出任务定义")
    @ApiOperation("导出任务定义")
    @PostMapping(value = "/download")
    public ResponseEntity<Object> download(@RequestBody CronTaskDefQueryVo queryVo) throws IOException {
        log.info("【CronTaskDefController.download】导出定时任务定义，参数:{}", queryVo.toString());
        String filePath = cronTaskDefService.exportExcel(converter.toCronTaskDefDto(queryVo));
        byte[] bytes = Files.readAllBytes(new File(filePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=test2.zip")
                .body(bar);
    }
}
